package com.huifaapp.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.JDred_packetAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.JDcomjPlistBean;
import com.huifaapp.mall.bean.JDlianjie;
import com.huifaapp.mall.bean.JDred_packetBean;
import com.huifaapp.mall.bean.Jdred_packetlistBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.Contig;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.support.v7.widget.LinearLayoutManager.*;

/**
 * Created by duanyu on 2018/1/29.
 */

public class JDred_packetActivity extends AppCompatActivity {

    private XRecyclerView activity_jdred_packet_xRecyclerView;
    private String shopid;
    private  String SPUEMPTYMSGURL= ApiManager.BASE_NIUPAI_URL+"goods/getSpuEmptyMsg";
    private  String JINGXUANLIST=ApiManager.BASE_NIUPAI_URL+"goods/getJingxuanList";
    private  String SYNCJDSPREADURL=ApiManager.BASE_NIUPAI_URL+Contig.typex+"/getUrl";//京东普通二合一链接
    private  String GETCONURLURL=ApiManager.BASE_NIUPAI_URL+Contig.typex+"/getConUrl";//京东获取优惠劵二合一链接
    private ImageView activity_jdred_packet_imge;
    private TextView activity_jdred_packet_goodsName;
    private TextView activity_jdred_packet_price;
    private TextView activity_jdred_packet_conprice;
    private View recyclerViewHOW;
    private List<Jdred_packetlistBean.DataBean.ListBean> listBeanlist;
    private JDred_packetAdapter jDred_packetAdapter;
    private LinearLayout activity_jdred_packet_cankanxiangqing;
    private JDred_packetBean jDred_packetBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jdred_packet_itme);
        Intent intent=getIntent();
        shopid = intent.getStringExtra("shopid");
        initView();
        initHttp();
        initlsitHttp();
    }

    private void initlsitHttp() {
        OkHttpUtils.post().addParams("","").url(JINGXUANLIST).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Gson gson=new Gson();
                Jdred_packetlistBean jdred_packetlistBean = gson.fromJson(response, Jdred_packetlistBean.class);
                if (jdred_packetlistBean.getCode().equals("10000")){
                    listBeanlist = new ArrayList<Jdred_packetlistBean.DataBean.ListBean>();
                    listBeanlist.addAll(jdred_packetlistBean.getData().getList());
                    jDred_packetAdapter = new JDred_packetAdapter(listBeanlist,JDred_packetActivity.this);
                    activity_jdred_packet_xRecyclerView.setLayoutManager(new GridLayoutManager(JDred_packetActivity.this, 2, GridLayoutManager.VERTICAL, false));
                    activity_jdred_packet_xRecyclerView.setAdapter(jDred_packetAdapter);
                    jDred_packetAdapter.setmMyItemclickListener(new JDred_packetAdapter.MyItemclickListener() {
                        @Override
                        public void itemclick(View view, final int position) {
                            Toast.makeText(JDred_packetActivity.this,"dianji",Toast.LENGTH_LONG).show();
                                if (Contig.typex.equals("jd")) {
                                    if (listBeanlist.get(position).getCouponMoney().equals("")) {
                                        Map<String,String>map=new HashMap<String, String>();
                                        map.put("spuId",listBeanlist.get(position).getId());
                                        map.put("appid", AUniqueIdentifier.getDeviceId(JDred_packetActivity.this));
                                        map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                        Log.e("京东二合一转链",SYNCJDSPREADURL+"?spuId="+listBeanlist.get(position).getId()+"&appId="+AUniqueIdentifier.getDeviceId(JDred_packetActivity.this)+"&token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                        OkHttpUtils.post().params(map).url(SYNCJDSPREADURL).build().execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {

                                            }

                                            @Override
                                            public void onResponse(String response) {
                                                Log.e("fgdgfe",response);
                                                Gson gson=new Gson();
                                                JDlianjie jDlianjie = gson.fromJson(response, JDlianjie.class);
                                                Log.e("kfjsdkghdfk", jDlianjie.getData());
                                                Intent intent = new Intent(JDred_packetActivity.this, HtmlActivity.class);
                                                intent.putExtra("data",jDlianjie.getData());
                                                intent.putExtra("shopid", listBeanlist.get(position).getId());
                                                startActivity(intent);
                                                finish();
                                            }
                                        });

                                    }
                                    if (listBeanlist.get(position).getCouponMoney()!=""){
                                        if (listBeanlist.get(position).getCouponMoney().equals("0.0")){
                                            Map<String,String> map=new HashMap<>();
                                            map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                            map.put("spuId",listBeanlist.get(position).getId());
                                            map.put("appid", AUniqueIdentifier.getDeviceId(JDred_packetActivity.this));
                                           // Log.e("优惠二和一链接接口",GETCONURLURL+"?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&spuId="+jDcomjPlistlist.get(location).getId()+"&appid="+AUniqueIdentifier.getDeviceId(getActivity()));
                                            OkHttpUtils.post().params(map).url(GETCONURLURL).build().execute(new StringCallback() {
                                                @Override
                                                public void onError(Request request, Exception e) {

                                                }

                                                @Override
                                                public void onResponse(String response) {
                                                    Log.e("优惠二和一数据",response);
                                                    Gson gson=new Gson();
                                                    JDlianjie jDlianjie = gson.fromJson(response, JDlianjie.class);

                                                    if (jDlianjie.getCode().equals("10000")) {
                                                        Intent intent=new Intent(JDred_packetActivity.this,JDred_packetActivity.class);
                                                        intent.putExtra("shopid",listBeanlist.get(position).getId());
                                                        startActivity(intent);
                                                        finish();
                                                    }
                                                }
                                            });

                                        }else {
                                            Map<String,String>map=new HashMap<>();
                                            map.put("token",SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                            map.put("spuId",listBeanlist.get(position).getId());
                                            map.put("appid", AUniqueIdentifier.getDeviceId(JDred_packetActivity.this));

                                            OkHttpUtils.post().params(map).url(GETCONURLURL).build().execute(new StringCallback() {
                                                @Override
                                                public void onError(Request request, Exception e) {

                                                }

                                                @Override
                                                public void onResponse(String response) {
                                                    Log.e("优惠二和一数据",response);
                                                    Gson gson=new Gson();
                                                    JDlianjie jDlianjie = gson.fromJson(response, JDlianjie.class);
                                                    jDlianjie.getData();
                                                    if (jDlianjie.getCode().equals("10000")) {
                                                        if (jDlianjie.getData()==null){
                                                            Intent intent = new Intent(JDred_packetActivity.this, HtmlActivity.class);
                                                            intent.putExtra("data", jDlianjie.getData());
                                                            intent.putExtra("shopid", listBeanlist.get(position).getId());
                                                            startActivity(intent);
                                                            finish();

                                                        }
                                                    }
                                                }
                                            });


                                        }
                                    }



                            }
                        }
                    });
                }
            }
        });
    }

    private void initHttp() {
        OkHttpUtils.post().addParams("id",shopid).url(SPUEMPTYMSGURL).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Gson gson=new Gson();
                jDred_packetBean = gson.fromJson(response, JDred_packetBean.class);
                if (jDred_packetBean.getCode().equals("10000")){
                    Glide.with(JDred_packetActivity.this).load(jDred_packetBean.getData().getMainImageUrl()).into(activity_jdred_packet_imge);
                    activity_jdred_packet_goodsName.setText(jDred_packetBean.getData().getGoodsName());
                    activity_jdred_packet_price.setText("¥"+"\t"+ jDred_packetBean.getData().getPrice());
                    activity_jdred_packet_conprice.setText("¥"+"\t"+ jDred_packetBean.getData().getConPrice());
                }
            }
        });

    }

    private void initView() {
        recyclerViewHOW = LayoutInflater.from(this).inflate(R.layout.activity_jdred_packet, null);
        activity_jdred_packet_xRecyclerView = (XRecyclerView) findViewById(R.id.activity_jdred_packet_XRecyclerView);
        activity_jdred_packet_xRecyclerView.addHeaderView(recyclerViewHOW);
        activity_jdred_packet_imge = (ImageView) recyclerViewHOW.findViewById(R.id.activity_jdred_packet_imge);
        activity_jdred_packet_goodsName = (TextView) recyclerViewHOW. findViewById(R.id.activity_jdred_packet_goodsName);
        activity_jdred_packet_price = (TextView) recyclerViewHOW. findViewById(R.id.activity_jdred_packet_price);
        activity_jdred_packet_conprice = (TextView)  recyclerViewHOW.findViewById(R.id.activity_jdred_packet_conprice);
        activity_jdred_packet_cankanxiangqing = (LinearLayout) recyclerViewHOW.findViewById(R.id.activity_jdred_packet_cankanxiangqing);
        activity_jdred_packet_cankanxiangqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String,String>map=new HashMap<String, String>();
                map.put("spuId",jDred_packetBean.getData().getSpuId());
                map.put("appid", AUniqueIdentifier.getDeviceId(JDred_packetActivity.this));
                map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                OkHttpUtils.post().params(map).url(SYNCJDSPREADURL).build().execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        Gson gson=new Gson();
                        JDlianjie jDlianjie = gson.fromJson(response, JDlianjie.class);;
                        Intent intent = new Intent(JDred_packetActivity.this, HtmlActivity.class);
                        intent.putExtra("data",jDlianjie.getData());
                        intent.putExtra("shopid",jDred_packetBean.getData().getSpuId());
                        startActivity(intent);
                        finish();
                    }
                });
            }
        });
    }
}
